import pygame, sys, math
pygame.init()

from Ball import Ball
from PlayerBall import PlayerBall
from Button import Button

clock = pygame.time.Clock()

width = 1280
height = 640
size = width, height

screen = pygame.display.set_mode(size)

bgColor = r,g,b = 0,0,0

ball1 = Ball("ball.png", [4,4], [100,100], [700,500])
ball2 = Ball("ball.png", [3,3], [75,75], [400,200])
ball3 = Ball("ball.png", [5,8], [125,125], [400,550])
ball4 = Ball("ball.png", [8,8], [50,50], [250,100])
ball5 = Ball("ball.png", [8,8], [50,50], [50,300])
ballp = PlayerBall(["playerBall1.png", 
                    "playerBall2.png", 
                    "playerBall3.png"] ,[9,9], [25,25], [1000,500])

while True:
    while ballp.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    ballp.direction("right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    ballp.direction("left")
                if event.key == pygame.K_w or event.key == pygame.K_UP:
                    ballp.direction("up")
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    ballp.direction("down")
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    ballp.direction("stop right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    ballp.direction("stop left")
                if event.key == pygame.K_w or event.key == pygame.K_UP:
                    ballp.direction("stop up")
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    ballp.direction("stop down")
            
        
        ball1.update()
        ball2.update()
        ball3.update()
        ball4.update()
        ball5.update()
        ballp.update()
        
        ball1.collideWall(width, height)
        ball2.collideWall(width, height)
        ball3.collideWall(width, height)
        ball4.collideWall(width, height)
        ball5.collideWall(width, height)
        ballp.collideWall(width, height)
        
        ball1.collideBall(ball2)
        ball1.collideBall(ball3)
        ball1.collideBall(ball4)
        ball2.collideBall(ball3)
        ball2.collideBall(ball4)
        ball3.collideBall(ball4)
        ball1.collideBall(ball5)
        ball2.collideBall(ball5)
        ball3.collideBall(ball5)
        ball4.collideBall(ball5)
        
        ballp.collideBall(ball1)
        ballp.collideBall(ball2)
        ballp.collideBall(ball3)
        ballp.collideBall(ball4)
        ballp.collideBall(ball5)
        
        screen.fill(bgColor)
        screen.blit(ball1.image, ball1.rect)
        screen.blit(ball2.image, ball2.rect)
        screen.blit(ball3.image, ball3.rect)
        screen.blit(ballp.image, ballp.rect)
        screen.blit(ball4.image, ball4.rect)
        screen.blit(ball5.image, ball5.rect)
        pygame.display.flip()
        clock.tick(60)
        
    restartButton = Button('RestartB.png', [200,100],[400,400])

    while not ballp.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
      
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if restartButton.collidePoint(event.pos):
                        ballp.living = True
                        ballp.place((1000,500))
                        ball1.place((750,500))
                        ball2.place((400,200))
                        ball3.place((400,550))
                        ball4.place((250,100))
                        ball5.place((50,300))
                        
        bGImage = pygame.image.load("Background2.png")
        bGRect = bGImage.get_rect()
        screen.blit(bGImage, bGRect)
        screen.blit(restartButton.image, restartButton.rect)
        pygame.display.flip()
        
        pygame.display.update()
        clock.tick(60)
    
        
    
    
    
    
    
    

#Restart button Code
